package com.lqq.session;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lqq
 * @date 2019/3/4
 */
public class SessionStore {
    private Hessian2Serialization h2Serialization=new Hessian2Serialization();
    //private RedisUtil redisUtil;
    private Jedis jedis=new Jedis("192.168.0.104", 6379);
    public Map<String,Object> getSession(String sid){
        Map<String,Object> session=new HashMap<String, Object>();
        try {
            byte[] bs = jedis.get(sid.getBytes());
            if(bs!=null){
                Object deserialize = h2Serialization.deserialize(bs);
                session=(Map<String, Object>) deserialize;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return session;
    }
    public void saveSession(String sid,Map<String, Object> session){
        try {
            byte[] bs = h2Serialization.serialize(session);
            jedis.set(sid.getBytes(), bs);
            jedis.expire(sid.getBytes(), 3600);//设置过期时间
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public void removeSession(String sid){
        try {
            jedis.del(sid.getBytes());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}